package com.app.reptile.tasks.selenium;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.app.reptile.tasks.AbstractDriverTask;
import com.app.reptile.utils.CrawlerUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;

public class WwkService extends AbstractDriverTask {

    public WwkService(){
    }

    public static void main(String[] args) throws Exception {
        WwkService wwkService = new WwkService();
        wwkService.start();
    }

    @Override
    protected void start(ChromeDriver chromeDriver) throws Exception {
        SSLContext sslContext = SSLContext.getInstance("SSL");

        TrustManager trustManager = new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

            }

            @Override
            public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        };
        TrustManager[] trustManagers = {trustManager};
        sslContext.init(null, trustManagers, new SecureRandom());

        HttpClient httpClient = HttpClient.newBuilder().sslContext(sslContext).build();

        String url = "https://wwk.aicangjia.com/api/518df3/movie/home.json?IC=b7adb5";
        HttpRequest requestBuilder = HttpRequest.newBuilder().uri(URI.create(url))
                .GET().build();
        HttpResponse<String> httpResponse = httpClient.send(requestBuilder, HttpResponse.BodyHandlers.ofString());

        String str = CrawlerUtils.trimJquery(httpResponse.body(),'{').toString();

        logger.info(str);

        JSONObject object = JSONObject.parseObject(str);
        JSONObject ret = object.getJSONArray("ret").getJSONObject(0);
        JSONArray list = ret.getJSONArray("list");

        for(int index = 0; index < list.size(); index ++){
            JSONObject obj = list.getJSONObject(index);
            String id = obj.getString("id");
            String name = obj.getString("name");
            String model = obj.getString("model");
            String pv = obj.getString("pv");
            String type = obj.getString("type");
            String num = obj.getString("num");

            String path = "https://wwk.aicangjia.com/app/#/type?model="+model+"&tid="+id;

            chromeDriver.get(path);

            List<WebElement> webElements = chromeDriver.findElements(By.cssSelector("ul.nav-list dfr flex-wrap li a"));


        }

    }
}
